package com.sky.aspect;

import com.sky.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
@Aspect
public class AutoClearRedis {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotion.AutoClearRedis)")
    public void autoClearRedisPointCut(){

    }
    @Before("autoClearRedisPointCut()")
    public void autoClearRedis(JoinPoint joinPoint){
        log.info("清理缓存");
        RedisUtils redisUtils = new RedisUtils(redisTemplate);
        List<String> keys = redisUtils.getKeysWithPrefix("dish_");
        Long delete = redisTemplate.delete(keys);
        log.info("清理{}条数据",delete);
    }
}
